<?php
/*
 * 用药状态信息编辑及展示接口
 * */
class Apiv1_MedicineController extends Ctrl_Apiatk{
    //编辑    
    public function editAction(){
        $p = $_REQUEST;
        $pData = empty($p['data'])?Tool_Fnc::ajaxMsg('数据错误'):$p['data'];
        $pType = empty($p['type'])?Tool_Fnc::ajaxMsg('用药状态不能为空'):$p['type'];
        //$pId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        //$pName = empty($p['name'])?Tool_Fnc::ajaxMsg('药品名称不能为空'):Tool_Fnc::safe_string($p['name']);
        //$pStatus = empty($p['status'])?Tool_Fnc::ajaxMsg('用药状态不能为空'):Tool_Fnc::safe_string($p['status']);
        //$pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('时间点不能为空'):Tool_Fnc::safe_string($p['at']);
        //$pData = urldecode($pData);

        //$tDatas = stripslashes(json_decode($pData));
        /*$tArr = $this->analyJson($pData);
        if(!$tArr){
            Tool_Fnc::ajaxMsg('json格式错误');
        }*/
        $tDatas = json_decode($pData,true);

        $tSMO = new SeniorModel;
        $tMMO = new MedicineModel;

        foreach($tDatas as $tRow){
            $tId = $tSMO->field('id')->where('id = '.$tRow['sid'])->fRow()['id'];
            if(empty($tId)){Tool_Fnc::ajaxMsg('该老人不存在');}
            $tTime = time();
        
            $tMid = $tMMO->field('id,created')->where('sid = '.$tRow['sid'].' and at = '.$tRow['at'].' and ml_id = "'.$tRow['id'].'"')->fRow(); 
            if(empty($tMid)){//记录为空，添加操作
                Tool_Fnc::ajaxMsg('该药品不存在');
            }else{//记录存在，修改操作
                $tData = array(
                    'status' => $tRow['medicine_status'],
                    'type' => $pType,
                    'created' => $tTime,
                    'date' => date('Y-m-d',$tTime),
                    'name' => $tRow['name'],
                    'sid' => $tRow['sid'],
                    'at' => $tRow['at'],
                    'id' => $tMid['id'],
                );
                if(!$tMMO->update($tData)){
                    Tool_Fnc::ajaxMsg('编辑失败 002');
                }
            } 
        }

        Tool_Fnc::ajaxMsg('编辑成功',1);
    }
    //展示
    public function listAction(){
        $p = $_REQUEST;
        $pId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $pDate = empty($p['date'])?'':Tool_Fnc::safe_string($p['date']);
        $pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('时间点不能为空'):Tool_Fnc::safe_string($p['at']);
        $pType = empty($p['type'])?Tool_Fnc::ajaxMsg('用药类型不能为空'):Tool_Fnc::safe_string($p['type']);
        $tSMO = new SeniorModel;
        $tId = $tSMO->field('id')->where('id = '.$pId)->fRow()['id'];
        if(empty($tId)){Tool_Fnc::ajaxMsg('该老人不存在');}

        $tMMO = new MedicineModel;
        $tMLMO = new MedicinelibraryModel;
        $tImgUrl = Yaf_Registry::get("config")->web->url->img;

        //默认查询今天数据
        $tWhere = ' sid = '.$pId.' and at = '.$pAt.' and type = '.$pType;
        if(!empty($pDate)){
           $tWhere = ' sid = '.$pId.' and at = '.$pAt.' and type = '.$pType; 
        }
        //$tDatas = $tMMO->field('id,name,sid,status medicine_status,date')->where($tWhere)->order('id desc')->fList();
        $tSql = 'select concat(\''.$tImgUrl.'\',img) img,ml.id id,m.at at,m.sid sid,ml.name name,m.status medicine_status,m.date date from '.$tMMO->table.' m left join '.$tMLMO->table.' ml on m.ml_id = ml.id where '.$tWhere.' order by m.id desc';
        $tDatas = $tMMO->query($tSql);
        foreach($tDatas as $tKey=>$tRow){
            $tDatas[$tKey]['medicine_status'] = '0';
        }
        $str = json_encode($tDatas);
        //if(empty($tDatas)){
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        //}
    }
    //获取周几
    private function getTimeWeek($time, $i = 0) {
        $weekarray = array('1', '2', '3', "4", "5", "6", "7");
        $oneD = 24 * 60 * 60;
        return $weekarray[date("w", $time + $oneD * ($i-1))];
    }
    //判断是不是合法json
    private function analyJson($json_str) {
            $json_str = str_replace('＼＼', '', $json_str);
            $out_arr = array();
            preg_match('/{.*}/', $json_str, $out_arr);
            if (!empty($out_arr)) {
                    $result = json_decode($out_arr[0], TRUE);
            } else {
                    return FALSE;
            }
            return $result;
    }
}
?>
